package com.zhangmeng.tools.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.zhangmeng.tools.utils.AlertUtils;
import com.zhangmeng.tools.utils.HttpUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.util.Callback;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-03-08 10:21
 */
@Slf4j
public class HttpRequestController {

    public enum Type{
        GET,
        POST;
    }

    @FXML
    public Button commit;

    @FXML
    public ListView<Data> request_params;

    @FXML
    public TextField key;

    @FXML
    public TextField value;

    @FXML
    public Button add_params;

    @FXML
    public TextField url;

    @FXML
    public ComboBox<Type> request_type;

    @FXML
    public TextArea request_result;

    private ObservableList<Data> list = FXCollections.observableArrayList();

    @FXML
    public void initialize() {
        request_params.setFixedCellSize(40);
        request_params.setEditable(true);
        request_result.setWrapText(true);
        request_params.setCellFactory(new Callback<>() {
            @Override
            public ListCell<Data> call(ListView<Data> param) {
                return new ListCell<>(){
                    @Override
                    protected void updateItem(Data s, boolean b) {
                        super.updateItem(s, b);
                        if (!b){
                            HBox hBox = new HBox();
                            hBox.setAlignment(Pos.CENTER);
                            hBox.getChildren().add(new Label(s.key + "  <======>  " + s.value));
                            this.setGraphic(hBox);
                        }
                    }
                };
            }
        });

        request_type.setItems(FXCollections.observableList(Arrays.stream(Type.values()).toList()));
        request_type.getSelectionModel().select(0);

        request_params.setItems(list);

        add_params.setOnAction(event -> {
            if (key.getText().length() == 0 ){
                AlertUtils.alert_warning("参数key不能为空!");
                return;
            }
            if (value.getText().length() == 0 ){
                AlertUtils.alert_warning("参数value不能为空!");
                return;
            }
            list.add(new Data(key.getText(),value.getText()));
        });

        commit.setOnAction(event -> {

            if (url.getText().length() == 0 ){
                AlertUtils.alert_warning("请求url不能为空!");
                return;
            }

            Type type = request_type.getSelectionModel().getSelectedItem();

            ObservableList<Data> items = request_params.getItems();
            Map<String,Object> map = new HashMap<>();
            for (Data item : items) {
                map.put(item.key,item.value);
            }
            log.info("请求参数:{}",map);
            request_result.setText(null);
            if (type.equals(Type.GET)){
                if (map.entrySet().size()>0){
                   request_result.setText(HttpUtils.get_request(url.getText(),map));
                }else {
                    request_result.setText(HttpUtils.get_request(url.getText()));
                }
            }
            if (type.equals(Type.POST)){
                if (map.entrySet().size()>0){
                    request_result.setText(HttpUtils.post_request(url.getText(),map));
                }else {
                    request_result.setText(HttpUtils.post_request(url.getText(),null));
                }
            }
        });
    }

    public static class Data{
        private String key;
        private String value;

        public Data(String key, String value) {
            this.key = key;
            this.value = value;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}
